package com.ubo.main;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javafx.embed.swing.SwingFXUtils;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.LabelBuilder;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.ToolBar;
import javafx.scene.effect.BlendMode;
import javafx.scene.effect.Reflection;
import javafx.scene.image.WritableImage;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Popup;

import javax.imageio.ImageIO;

import com.ubo.java.generator.printer.FilePrinter;

/**
 * 
 * @author Guillaume
 * 
 */
public class MainView {

	protected BorderPane mLayout;

	protected HBox mCenterBox;

	protected ToolBar mToolBarTop;

	protected ToolBar mToolBarBot;

	protected Pane mLeftPane;

	protected Pane mRightPane;

	protected Button mButtonUSpeed;

	protected Button mButtonXML;

	protected Button mButtonJava;

	protected Button mButtonDot;

	protected Button mButtonVisu;

	protected Button mButtonExport;

	protected Button mButtonConvert;

	protected TextArea mLeftArea;

	protected MainGenerator mGenerator;

	protected TextArea mRightArea;
	
	protected EGenerationType mCurrGenerationType;

	public MainView() {
		mLayout = new BorderPane();
		mLayout.setId("background");
		mCenterBox = new HBox();
		mGenerator = new MainGenerator();
		mCenterBox.setAlignment(Pos.CENTER);
		buildView();
	}

	/**
	 * Construction de la vue.
	 */
	protected void buildView() {

		mToolBarTop = new ToolBar();
		mToolBarBot = new ToolBar();

		Region spacer = new Region();
		spacer.getStyleClass().setAll("spacer");
		HBox buttonBar = new HBox();
		buttonBar.getStyleClass().setAll("segmented-button-bar");
		mButtonXML = new Button("XML");
		mButtonXML.getStyleClass().addAll("first");
		mButtonXML.setOnMouseClicked(new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent arg0) {
				launchGeneration(EGenerationType.XML);
			}
		});
		mButtonJava = new Button("Java");
		mButtonJava.setOnMouseClicked(new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent arg0) {
				launchGeneration(EGenerationType.JAVA);
			}
		});
		mButtonDot = new Button("DOT");
		mButtonDot.setOnMouseClicked(new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent arg0) {
				launchGeneration(EGenerationType.DOT);
			}
		});
		mButtonVisu = new Button("Graphique");
		mButtonVisu.getStyleClass().addAll("last", "capsule");
		mButtonVisu.setOnMouseClicked(new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent arg0) {
				launchGeneration(EGenerationType.VISU);
			}
		});
		buttonBar.getChildren().addAll(mButtonXML, mButtonJava, mButtonDot,
				mButtonVisu);

		Reflection r = new Reflection();
		r.setFraction(0.8f);

		Label labelTop = LabelBuilder.create().labelFor(mToolBarTop)
				.text("Modes de conversion ").textFill(Color.LIME).effect(r)
				.font(Font.font(null, FontWeight.BOLD, 16)).build();
		mToolBarTop.getItems().addAll(spacer, labelTop, buttonBar);

		spacer = new Region();
		spacer.getStyleClass().setAll("spacerBot");
		buttonBar = new HBox();
		buttonBar.getStyleClass().setAll("segmented-button-bar");
		mButtonExport = new Button("Exporter");
		mButtonExport.setOnMouseClicked(new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent arg0) {
				FileChooser chooserStage = new FileChooser();
				File newFile;
				if(!mCurrGenerationType.equals(EGenerationType.VISU)) {
					if(mCurrGenerationType.equals(EGenerationType.DOT)) {
						chooserStage.getExtensionFilters().add(new ExtensionFilter("DOT", "*.dot"));
						newFile =  chooserStage.showSaveDialog(new Popup());
					} else if(mCurrGenerationType.equals(EGenerationType.DOT)) {
						chooserStage.getExtensionFilters().add(new ExtensionFilter("Java", "*.java"));
						newFile =  chooserStage.showSaveDialog(new Popup());
					} else {
						chooserStage.getExtensionFilters().add(new ExtensionFilter("XML", "*.xml"));
						newFile =  chooserStage.showSaveDialog(new Popup());
					}
					newFile =  chooserStage.showSaveDialog(new Popup());
					FilePrinter.printFile(newFile, mRightArea.getText());
				} else {
					chooserStage.getExtensionFilters().add(new ExtensionFilter("PNG", "*.png"));
					newFile =  chooserStage.showSaveDialog(new Popup());
					WritableImage wi =  mRightPane.snapshot(new SnapshotParameters(), null);
					BufferedImage bImage = SwingFXUtils.fromFXImage(wi, null);
					try {
						ImageIO.write(bImage, "png", newFile);
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		});
		mButtonExport.getStyleClass().addAll("only", "capsule");
		buttonBar.getChildren().addAll(spacer, mButtonExport);
		mToolBarBot.getItems().addAll(buttonBar);

		mRightPane = new Pane();
		mLeftPane = new Pane();
		mLeftArea = new TextArea(
				"Rentrez ici le texte USpeed � convertir puis cliquez sur l'un des boutons de conversion");
		mLeftArea.setBlendMode(BlendMode.DIFFERENCE);

		mRightArea = new TextArea();
		mRightArea.setBlendMode(BlendMode.DIFFERENCE);

		mLeftPane.getChildren().add(mLeftArea);

		ScrollPane leftScrollPane = new ScrollPane();
		leftScrollPane.setContent(mLeftPane);
		ScrollPane rightScrollPane = new ScrollPane();
		rightScrollPane.setContent(mRightPane);
		mLeftArea.prefWidthProperty().bind(leftScrollPane.widthProperty());
		mLeftArea.prefHeightProperty().bind(leftScrollPane.heightProperty());

		mRightArea.prefWidthProperty().bind(leftScrollPane.widthProperty());
		mRightArea.prefHeightProperty().bind(leftScrollPane.heightProperty());

		mLayout.setTop(mToolBarTop);
		mLayout.setCenter(mCenterBox);
		mLayout.setBottom(mToolBarBot);

		mCenterBox.getChildren().addAll(leftScrollPane, rightScrollPane);
		HBox.setHgrow(leftScrollPane, Priority.ALWAYS);
		HBox.setHgrow(rightScrollPane, Priority.ALWAYS);
		mCenterBox.setMinSize(0, 0);
	}

	/**
	 * Getter du borderPane principal.
	 * 
	 * @return
	 */
	public Pane getMainPane() {
		return mLayout;
	}

	/**
	 * 
	 * @param generationType
	 */
	public void launchGeneration(EGenerationType generationType) {
		mRightPane.getChildren().clear();
		switch (generationType) {
		case DOT:
			mRightPane.getChildren().add(mRightArea);
			mRightArea.clear();
			mRightArea.setText(mGenerator.generateDotFromUSpeed(mLeftArea.getText()));
			mCurrGenerationType = EGenerationType.DOT;
			break;
		case JAVA:
			mRightPane.getChildren().add(mRightArea);
			mRightArea.clear();
			mRightArea.setText(mGenerator.generateJavaFromUSpeed(mLeftArea.getText()));
			mCurrGenerationType = EGenerationType.JAVA;
			break;
		case VISU:
			mRightPane.getChildren().add(mGenerator.generateVisuFromUSpeed(mLeftArea.getText()));
			mCurrGenerationType = EGenerationType.VISU;
			break;
		case XML:
			mRightPane.getChildren().add(mRightArea);
			mRightArea.setText(mGenerator.generateXML(mLeftArea.getText()));
			mCurrGenerationType = EGenerationType.XML;
			break;
		default:
			break;
		}
	}

}
